import Mock from "mockjs";

// 提取公共图片路径
const basePicUrl = "/img/business";

// 生成随机商品名称组件
const foodTypes = ["大盘鸡米饭", "土豆粉", "米线", "蟹煲", "鸡爪煲", "麻辣拌", "麻辣烫", "麻辣香锅", "炒米粉", "汉堡"];
const shopNames = ["宁大云创店", "智慧城店", "宁波大学店", "庄市店", "高新区店", "学府店"];
const prefixes = ["疯狂的", "老巷子", "伍小", "小谷姐姐", "钱锅锅", "馋兜兜", "怪兽堡", "胖妈妈"];

// 生成随机商品数据
function generateRandomFoodList(count = 20) {
  const result = [];
  
  for (let i = 0; i < count; i++) {
    // 随机选择名称组件
    const foodType = foodTypes[Mock.Random.integer(0, foodTypes.length - 1)];
    const shopName = shopNames[Mock.Random.integer(0, shopNames.length - 1)];
    const prefix = prefixes[Mock.Random.integer(0, prefixes.length - 1)];
    
    // 随机生成标题
    let title = `${prefix}${foodType}`;
    if (Mock.Random.boolean()) {
      title += `(${shopName})`;
    }
    
    // 随机生成配送信息
    const deliveryFee = Mock.Random.integer(0, 5);
    const deliveryText = deliveryFee === 0 ? "免配送费" : `配送约￥${deliveryFee}`;
    
    // 随机生成月销量
    const soldCount = Mock.Random.integer(100, 5000);
    
    // 随机生成价格范围（根据商品类型略有不同）
    let minPrice, maxPrice;
    if (foodType.includes("饭") || foodType.includes("粉") || foodType.includes("面")) {
      minPrice = 10;
      maxPrice = 25;
    } else if (foodType.includes("煲") || foodType.includes("锅")) {
      minPrice = 20;
      maxPrice = 40;
    } else {
      minPrice = 15;
      maxPrice = 30;
    }
    
    result.push({
      id: i + 1,
      title,
      pic: `${basePicUrl}${i % 9 + 1}.jpg`,
      spec: `起送￥20 ${deliveryText}`,
      sold_count: `月售${soldCount}`,
      price: Mock.Random.float(minPrice, maxPrice, 2, 2),
      // 新增评分属性
      rating: Mock.Random.float(3.5, 5.0, 1, 1),
      // 新增标签属性
      tags: Mock.Random.shuffle(["新店特惠", "爆款推荐", "回头客多", "免费汤品"], 0, 2)
    });
  }
  
  return result;
}

// 生成20个商品数据
const foodList = generateRandomFoodList(20);

// 修改接口路径为 /product/page
Mock.mock(new RegExp('/product/page'), 'get', options => {
  console.log(options); // 测试用
  return {
    code: 1,
    data: foodList,
  };
});

// 详细用法参见官网 http://mockjs.com/    